package com.hnxxxy.sysmgr.service.impl;

import com.hnxxxy.common.AjaxRtnEntity.AjaxRtnEntity;
import com.hnxxxy.sysmgr.mapper.MenuMapper;
import com.hnxxxy.sysmgr.pojo.Menu;
import com.hnxxxy.sysmgr.service.MenuService;
import com.hnxxxy.sysmgr.vo.MenuVo;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Json
 * @date 2020-26-8:07
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 查询所有菜单
     * @param menuVo
     * @return
     */
    @Override
    public List<Menu> queryAllMenu(MenuVo menuVo) {

        List<Menu> data = this.menuMapper.queryAllMenu(menuVo);
        return data;
    }

    /**
     * 添加菜单
     * @param menuVo
     * @return
     */
    @Override
    public Integer addMenu(MenuVo menuVo) {
        try {
            menuMapper.insertMenu(menuVo);
            return AjaxRtnEntity.success;
        }catch (Exception e){
            e.printStackTrace();
            return AjaxRtnEntity.error;
        }
    }

    /**
     *查询所有菜单，并返回树形结构
     * @param menuVo
     * @return
     */
    @Override
    public List<Menu> queryAllMenuForList(MenuVo menuVo) {

        return menuMapper.queryAllMenu(menuVo);
    }

    /**
     * 修改菜单
     * @param menuVo
     */
    @Override
    public Integer updateMenu(MenuVo menuVo) {
        try {
            menuMapper.updateMenu(menuVo);
            return AjaxRtnEntity.success;
        }catch (Exception e){
            e.printStackTrace();
            return AjaxRtnEntity.error;
        }
    }

    /**
     * 删除菜单
     * @param id
     */
    @Override
    public Integer deleteMenu(Integer id) {
        try {
            menuMapper.deleteMenu(id);
            return AjaxRtnEntity.success;
        }catch (Exception e){
            return AjaxRtnEntity.error;
        }
    }

    /**
     * 根据pid查询菜单数量
     * @param pid
     * @return
     */
    @Override
    public Integer queryMenuByPid(Integer pid) {
        return this.menuMapper.queryMenuByPid(pid);
    }

    /**
     * 批量删除菜单
     * @param ids
     */
    @Override
    public Integer batchDeletemenu(Integer[] ids) {
        try {
            for (Integer id : ids) {
                deleteMenu(id);
            }
            return AjaxRtnEntity.success;
        }catch (Exception e){
            e.printStackTrace();
            return AjaxRtnEntity.error;
        }

    }

}
